<%@page import="multiface.web.util.Paginator"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>

<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<div class="box">
    <%
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        //Parametri della jsp
        String order = request.getParameter("order");
        String direction = request.getParameter("direction");
        String text = request.getParameter("text");
        String jsp = "search_view_part.jsp";
        String functionJS = "search";
        String indexs = request.getParameter("index");
        int index = 0;
        String pids = request.getParameter("pid");
        int pid = Integer.parseInt(pids);
        int part_id = pid;

        //Controlli sui parametri
        if (indexs != null) {
            index = Integer.parseInt(indexs);
        }
        if (direction == null) {
            direction = "ASC";
        }

        //Paginator
        Paginator paginator = new Paginator();
        paginator.setCSSClass("pager-list");
        paginator.setCSSClassCurrentPage("selected");
        paginator.setCurrentIndex(index);
        paginator.setElementsForPage_fromDB(true);
        paginator.setFunctionJS(functionJS);
        paginator.setPageReturn(jsp);

        //Set Parametri iniziali
        paginator.addParameter("index", indexs);
        paginator.addParameter("pid", pids);
        paginator.addParameter("text", text);

        List<Prodotto> list = new LinkedList<Prodotto>();
        if (order == null) {
            Query q = em.createNativeQuery("SELECT * FROM prodotto p WHERE p.ricambio=false AND (p.nome LIKE ? OR p.codice LIKE ?) AND p.idprodotto not in (SELECT h.prodotto_idprodotto FROM prodotto_has_ricambio h WHERE h.prodotto_idricambio=?) ORDER BY p.dataora DESC", Prodotto.class);
            q.setParameter(1, "%" + text + "%");
            q.setParameter(2, "%" + text + "%");
            q.setParameter(3, part_id);
            paginator.setTotalElements(q.getResultList().size());
            q.setFirstResult(index);
            q.setMaxResults(paginator.getElementsForPage());
            list = q.getResultList();
        } else {
            paginator.addParameter("order", order);
            if (direction.equals("ASC")) {
                paginator.addParameter("direction", "DESC");
                Query q = em.createNativeQuery("SELECT * FROM prodotto p WHERE p.ricambio=false AND (p.nome LIKE ? OR p.codice LIKE ?) AND p.idprodotto not in (SELECT h.prodotto_idprodotto FROM prodotto_has_ricambio h WHERE h.prodotto_idricambio=?) ORDER BY p." + order + " ASC", Prodotto.class);
                q.setParameter(1, "%" + text + "%");
                q.setParameter(2, "%" + text + "%");
                q.setParameter(3, part_id);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("direction", "ASC");
                Query q = em.createNativeQuery("SELECT * FROM prodotto p WHERE p.ricambio=false AND (p.nome LIKE ? OR p.codice LIKE ?) AND p.idprodotto not in (SELECT h.prodotto_idprodotto FROM prodotto_has_ricambio h WHERE h.prodotto_idricambio=?) ORDER BY p." + order + " DESC", Prodotto.class);
                q.setParameter(1, "%" + text + "%");
                q.setParameter(2, "%" + text + "%");
                q.setParameter(3, part_id);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            }
        }

        Iterator i = list.iterator();
        out.print("<table class=\"listing\" border=\"0\">");
        out.print("<tr>");
        out.print("<td>" + paginator.getFunction("order", "codice", "Code") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "nome", "Name") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "anno", "Year") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "dataora", "Date") + "</td>");
        out.print("<td colspan=\"4\">Fast Setting</td>");
        out.print("</tr>");
        while (i.hasNext()) {
            Prodotto p = (Prodotto) i.next();
            int prod_id = p.getIdprodotto();

            String img_promo = "notpromo.png";
            if (p.getOfferta()) {
                img_promo = "promo.png";
            }
            String img_home = "nothome.png";
            if (p.getHomepage()) {
                img_home = "home.png";
            }
            String img_vis = "unvisibile.png";
            if (p.getStato().equals("VISIBILE")) {
                img_vis = "visibile.png";
            }


            out.print("<tr id=\"search_prod_of_part_" + p.getIdprodotto() + "\"  >");
            out.print("<td class=\"codice\">" + p.getCodice() + "</td>");
            out.print("<td class=\"addpart\"><a href=\"javascript:add_prod_of_part(" + prod_id + "," + part_id + ");\">" + p.getNome() + "</a></td>");
            out.print("<td class=\"anno\">" + p.getAnno() + "</td>");
            out.print("<td class=\"dataora\">" + Util.FormatData(p.getDataora()) + "</td>");
            out.print("<td class=\"visibile\"><a title=\"Set Un/Visible\" href=\"javascript:visibile(" + prod_id + ");\"><img id=\"img_visibile_" + prod_id + "\" src=\"../engine/img/" + img_vis + "\"></img></a></td>");
            out.print("<td class=\"homepage\"><a title=\"Set to HomePage\" href=\"javascript:homepage(" + prod_id + ");\"><img id=\"img_homepage_" + prod_id + "\" src=\"../engine/img/" + img_home + "\"></img></a></td>");
            out.print("<td class=\"promo\"><a title=\"Set Promo\" href=\"javascript:promo(" + prod_id + ");\"><img id=\"img_promo_" + prod_id + "\" src=\"../engine/img/" + img_promo + "\"></img></a></td>");
            out.print("<td class=\"cancella\"><a title=\"Delete Part\" href=\"javascript:del('prod'," + prod_id + ");\"><img id=\"delete_part_" + prod_id + "\" src=\"../engine/img/delete.png\"></img></a></td>");
            out.print("</tr>");
        }
        out.print("</table>");
        em.close();
        //emf.close();

        //!!!IMPORTANTE!!!! rittere la direzione in quella della pagina attuale
        paginator.addParameter("direction", direction);
        out.print(paginator);
    %>

</div>
<script>
    $("tr").hover(
    function(){
        $(this).find(".td-name").append($("<div class=\"tr-edit\">EDIT<div>"));
    },
    function () {
        $(this).find(".td-name .tr-edit").remove();
    }
);
</script>



